package com.yufei.infoExtractor.core;

import it.sauronsoftware.cron4j.Scheduler;




import java.io.IOException;



import java.lang.reflect.Constructor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.timer.ScheduledTimerTask;

import com.yufei.infoExtractor.pfw.InfoExtractorDao;
import com.yufei.infoExtractor.pfw.entity.Hotel;
import com.yufei.infoExtractor.pfw.entity.Pattern;
import com.yufei.infoExtractor.pfw.entity.Seedsite;
import com.yufei.infoExtractor.pfw.entity.Task;
import com.yufei.infoExtractor.pfw.entity.Weibo;
import com.yufei.infoExtractor.pfw.impl.InfoExtractorDaoMongodImpl;
import com.yufei.infoExtractor.pfw.mongodb.DataRepository;
import com.yufei.infoExtractor.task.InfoExtractorCommonTask;
import com.yufei.infoExtractor.util.AppUtil;
import com.yufei.infoExtractor.util.CommonUtil;
import com.yufei.infoExtractor.util.ExceptionUtil;
import com.yufei.infoExtractor.util.PatternUtils;
/**
 * created by @author jasstion at 2012-9-17
 * 执行指定的task,必须有一个构造函数参数为：此次要执行的所有task信息
 */
public class TaskRunner {
	private static Log mLog = LogFactory.getLog(TaskRunner.class);
	private List<Task> readyExcuteTasks=new ArrayList<Task>();
    private Scheduler scheduler=null;
	public List<Task> getReadyExcuteTasks() {
		return readyExcuteTasks;
	}

	public void setReadyExcuteTasks(List<Task> readyExcuteTasks) {
		this.readyExcuteTasks = readyExcuteTasks;
	}

	public TaskRunner(List<Task> readyExcuteTasks) {
		super();
		if(CommonUtil.isEmptyOrNull(readyExcuteTasks)){
			throw new IllegalArgumentException();
		}
		this.readyExcuteTasks = readyExcuteTasks;
	}
	
    public void run(){
    	
    	List<InfoExtractorCommonTask> tasks=(List<InfoExtractorCommonTask>)convertTaskToCron4jTask(readyExcuteTasks);
    	scheduler=new Scheduler();
    	for(InfoExtractorCommonTask task:tasks){
    		if(CommonUtil.isEmptyOrNull(task.getTask().getCron())){
        		scheduler.launch(task);

    		}
    		scheduler.schedule(task.getTask().getCron(), task);
    	}
    	
    	
	
    }


	private List<? extends it.sauronsoftware.cron4j.Task> convertTaskToCron4jTask(
			List<Task> readyExcuteTasks2) {
		// TODO Auto-generated method stub
		List<it.sauronsoftware.cron4j.Task> tasks=new ArrayList<it.sauronsoftware.cron4j.Task>();
		it.sauronsoftware.cron4j.Task task=null;
		for(Task infoExtractorTask:readyExcuteTasks2){
			
			try {
				Class cron4jTaskClass=Class.forName(infoExtractorTask.getTaskExecuteJobFullName());
				Class[] pTypes={Task.class};
				Constructor constructor=cron4jTaskClass.getConstructor(pTypes);
				Object[] paraters={infoExtractorTask};
		    	task=(it.sauronsoftware.cron4j.Task)constructor.newInstance(paraters);
		    	tasks.add(task);


			} catch (Exception e) {
				mLog.info(ExceptionUtil.getExceptionDetailsMessage(e));
				}
			}
		return tasks;
	}	
	public static void main(String[] args){
		InfoExtractorDao cd=(InfoExtractorDao) AppUtil.getBeanFromBeanContainer(InfoExtractorDao.class);
	
		try {
			TaskRunner taskRunner=new TaskRunner(cd.getTasks());
			taskRunner.run();
			
		    
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}
}

